<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2017/8/30
 * Time: 9:56
 */
function check_active_time($begin_at,$end_at){
    $time = time();
    if($time < $begin_at){
        return ['code'=>120,'message'=>'活动未开始'];
    }
    if($time > $end_at){
        return ['code'=>121,'message'=>'活动已结束'];
    }
    return false;
}

function filterEmoji($str)
{
    $str = preg_replace_callback(
        '/./u',
        function (array $match) {
            return strlen($match[0]) >= 4 ? '' : $match[0];
        },
        $str);

    return $str;
}

function ip_to_address($ip){

    if($ip == '127.0.0.1' || $ip == '192.168.1.9'){
        return ['code'=>0,'message'=>'本机测试','ip'=>$ip];
    }

    $content = file_get_contents("http://api.map.baidu.com/location/ip?ak=w0kuwS3flsIHScAsluGck43V&ip=$ip");
    $json = json_decode($content,true);

    if($json['status'] == 0){
        return ['code'=>0,'message'=>$json['content']['address'],'ip'=>$ip];
    }else{
        return ['code'=>$json['status'],'message'=>$json['message'],'ip'=>$ip];
    }
}

function check_params($params){
    if(!empty($params['mobile'])){
        $not_mobile = not_mobile_phone($params['mobile']);
        if($not_mobile){
            return ['code'=>'111','message'=>'手机号码格式不正确'];
        }
    }

    //更多参数验证
    return false;
}

function not_mobile_phone($val){
    if($val){
        return preg_match('/^0?(13|15|18|14|17)[0-9]{9}$/',$val) ?
            false: '手机号码格式不正确';
    }else{
        return '手机号码不能为空';
    }

}

/**
 * 投票限制方法组入口
 * @param $functions
 * @param $params
 * @return mixed
 */
function check_function($functions,$params){

    foreach($functions as $function){
        if(function_exists($function)){
            $res = $function($params);
            if($res){
                return $res;
                break;
            }
        }
    }
    return false;
}
/************************************ 方法组 开始 ******************************************/

function limit_ip($params){

    if($params['ip_info']['code'] == 0){
        if(!empty($params['province']) && strpos($params['ip_info']['message'],$params['province']) === false ){
            return ['code'=>180,'message'=>'ip不属于'.$params['province'].'省'];
        }
        if(!empty($params['city']) && strpos($params['ip_info']['message'],$params['city']) === false ){
            return ['code'=>181,'message'=>'ip不属于'.$params['city'].'市'];
        }
    }else{
        return ['code'=>$params['ip_info']['code'],'message'=>$params['ip_info']['message']];
    }
    return false;
}
function vote_times($params){
;
    if(!empty($params['vote_times'])){
        $where = [
            //'participant_id'=>$params['participant_id'], // 针对参赛者投票限制
            'active_id'=>$params['active_id'],
            'user_id'=>$params['vote_user_id']
        ];
        $model = new \app\admin\model\VoteList();


       // if(in_array($params['active_id'],[25])){
            $where['create_time'] = ['>',strtotime(date('Y-m-d').' 00:00:00')];
            $count = $model->where($where)->count();
            
            if ($params['pass_way'] == 'yqsbz'){
               // unset($where['user_id']);
                // $where['from_device']=$params['device_id'];
                //$count2 = $model->where($where)->count();
                if($params['vote_times'] <= $count){
                    // return ['code'=>182,'message'=>'每台手机每日对每位参与者仅限投'.$params['vote_times'].'票'];
                    return ['code'=>182,'message'=>'每人每日限投'.$params['vote_times'].'票'];
                }
            }else{
                if($count >= 1){
                   // return ['code'=>182,'message'=>'每人每日对每位参与者仅限投'.$params['vote_times'].'票'];
                   return ['code'=>182,'message'=>'每人每日限投 1 票'];
                }
            }
       // }else{
            // $count = $model->where($where)->count();
            // if($params['vote_times'] <= $count){
            //     return ['code'=>182,'message'=>'每人对每位参与者仅限投'.$params['vote_times'].'票'];
            // }
            // if ($params['pass_way'] == 'yqsbz'){
            //     unset($where['user_id']);
            //     $where['from_device']=$params['device_id'];
            //     $count2 = $model->where($where)->count();
            //     if($params['vote_times'] <= $count2){
            //         return ['code'=>182,'message'=>'每台手机对每位参与者仅限投'.$params['vote_times'].'票'];
            //     }
            // }
      //  }


    }
    return false;
}

function ip_times($params){
    /*if($params['ip_info']['code'] == 0){
        $model = new \app\admin\model\VoteList();
        $count = $model->where('active_id',$params['active_id'])->where('user_id',$params['vote_user_id'])
            ->where('from_ip',$params['ip_info']['ip'].$params['ip_info']['message'])->count();
        if($params['ip_times'] <= $count){
            return ['code'=>183,'message'=>'来源ip投票过多,建议使用4G等网络投票'];
        }
    }else{
        return ['code'=>$params['ip_info']['code'],'message'=>$params['ip_info']['message']];
    }*/
    return false;
}




// 手机号码归属地
function phone_area($params){

    if(empty($params['user_phone_area']) || $params['user_phone_area'] != $params['phone_area']){
        // if(!empty($params['province']) && strpos($params['ip_info']['message'],$params['province']) === false ){
        //     return ['code'=>180,'message'=>'ip不属于'.$params['province'].'省'];
        // }
        //if(!empty($params['city']) && strpos($params['ip_info']['message'],$params['city']) === false ){
            return ['code'=>184,'message'=>'手机归属地不属于'.$params['phone_area'].'市'];
        //}
    }
    return false;
}

// 手机号码归属地
function get_phone_area($mobile){

    $url = "http://mobsec-dianhua.baidu.com/dianhua_api/open/location?tel={$mobile}";
    $ch = curl_init();
    curl_setopt($ch,CURLOPT_URL,$url);
   // curl_setopt($ch,CURLOPT_POST,1);
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
    $data = curl_exec($ch);
    curl_close($ch);
    $data = json_decode($data,true);

    $city = $data['response'][$mobile]['detail']['area'][0]['city'];
    return $city;
}

function wx_init(){

    $wxconfig = array();

    // vendor('wechat.class#jssdk');

    // $config = APP_DEBUG ? C("WECHAT_SDK_TEST"): C("WECHAT_SDK");
    // 这里配置了微信公众号的AppId和AppSecret

    $jssdk =new jssdk('wx3a8ae5ff0766195c', '7933680b91e82b4a9db124600fc52742');

    $wxconfig = $jssdk->GetSignPackage();

    return $wxconfig;

}



/************************************ 方法组 结束 ******************************************/
